using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Xml;
using System.Net;

namespace Dominio
{
    public class MailService
    {
        private String from;
        private String to;
        private String subject;
        private String message;
        private MailMessage mail;
        public MailService()
        {
            mail = new MailMessage();
        }

        public void sendMail(String mailType, String to)
        {
            this.to = to;
            XmlDocument xDoc = new XmlDocument();

            xDoc.Load(@"C:\Users\Jairo\Desktop\back up\Visual Studio 2005\Projects\AccesoADatos\Services\mailsType\WelcomeMail.xml");

            XmlNodeList mailList = xDoc.GetElementsByTagName("mails");

            XmlNodeList lista = ((XmlElement)mailList[0]).GetElementsByTagName("mail");

            foreach (XmlElement nodo in lista)
            {
                if (nodo.Attributes["type"].InnerText.Equals(mailType))
                {
                    
                    this.from = nodo.FirstChild.InnerText;
                    this.subject = nodo.ChildNodes[1].InnerText;
                    this.message = nodo.ChildNodes[2].InnerText;                   
                }
            }
            
            
            mail.From = new MailAddress(this.from);
            mail.To.Add(new MailAddress(this.to));
            mail.Subject = this.subject;
            mail.Body = this.message;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Credentials = new NetworkCredential("jairodemorais", "internet");

            try
            {
                smtp.EnableSsl = true;
                smtp.Send(mail);
                
            }
            catch(Exception ex)
            {
                throw new Exception();
            }

        }
        public String Message
        {
            get { return message; }
            set { message = value; }
        }
        public String To
        {
            get { return to; }
            set { to = value; }
        }
        public String From
        {
            get { return from; }
            set { from = value; }
        }
        public String Subject
        {
            get { return subject; }
            set { subject = value; }
        }
       
    }
}
